﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;


using System;
using System.Data;
using System.Drawing;
using System.Linq;

namespace ConfExpert.Forms.Planlama
{
    public partial class GomlekTransferi : XForm
    {
        public GomlekTransferi()
        {
            InitializeComponent();
        }

        private void GomlekTransferi_Load(object sender, EventArgs e)
        {
            DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5)");
            RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
            gleDepo.Edit.Assign(riDepo);
            DagilimChanged();
            gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;
            gleDepo.EditValueChanged += gleDepo_EditValueChanged;
            btnTransfer.Click += btnTransfer_Click;
            gridView1.RowCellStyle += gridView1_RowCellStyle;
            cmbTransfer.EditValueChanged += cmbTransfer_EditValueChanged;
            gridView1.RowStyle += gridView1_RowStyle;
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gleAktarılacakDepo.EditValueChanged += gleAktarılacakDepo_EditValueChanged;
            if (gleAktarılacakDepo.EditValue == null)
                btnTransfer.Enabled = false;
            else
                btnTransfer.Enabled = true;
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName.Length == 3 &&
                    e.Column.FieldName.StartsWith("A"))
                {
                    DataRow dr = gridView1.GetDataRow(e.RowHandle);
                    int D = intParse(dr["DagilimId"]);

                    e.Appearance.ForeColor = (DagilimId != D ? Color.LightSlateGray : Color.Black);
                }
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DagilimChanged();
        }

        void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle > -1)
                if ((sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") != null)
                    if ((sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") != DBNull.Value)
                        if ((bool)(sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") == true)
                            e.Appearance.BackColor = Color.SkyBlue;
                        else
                            e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);
        }

        void gleAktarılacakDepo_EditValueChanged(object sender, EventArgs e)
        {
            btnTransfer.Enabled = true;
        }

        void cmbTransfer_EditValueChanged(object sender, EventArgs e)
        {
            DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5)");
            RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
            if (cmbTransfer.EditValue.ToStr() == "Depo")
                gleAktarılacakDepo.Properties.Assign(riDepo);
            else if (cmbTransfer.EditValue.ToStr() == "Nakliyeci")
                gleAktarılacakDepo.Properties.Assign(RI(Rp.Nakliyeci));
        }

        void btnTransfer_Click(object sender, EventArgs e)
        {

            gridView1.EndEditing();
            if (gridView1.DataTable().Select("Seç=1").Count() == 0)
            {
                XtraMessageBox.Show("Lütfen transfer yapacağınız modelleri seçin!");
                return;
            }
            DataTable dt = gridView1.DataTable().Select("Seç=1").CopyToDataTable();
            dt.TableName = "GomlekStok";
            string xml = dt.Serialize();
            ExecuteNonQuery("UpdGomlekStok", dt.Serialize(), gleAktarılacakDepo.EditValue.ToInt(), gleDepo.EditValue.ToInt(), cmbTransfer.EditValue.ToString(), WhoAmI);

            // gridView1.VeriyiGuncelle("GetGomlekStok", gleDepo.EditValue);
            gridView1.Init("GetGomlekStok", gleDepo.EditValue);
            gridView1.BestFitColumns();
            gridView1.FormatAsNumber(0, "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "Numune", "Iptal", "Kalite2", "Kalite3", "Kalan1Kalite", "EksikAdet");

        }

        void gleDepo_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.Init("GetGomlekStok", gleDepo.EditValue);
            gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;
            GridManager gm = GridManager.GetManager(gridView1);
            DagilimChanged();
            gm.GridMenu.RemoveMenu("Seç");
            gm.GridMenu.RemoveMenu("Bırak");
            gridView1.EndEditing();
            gridView1.AddColumn("Seç", typeof(bool));
            gm.GridMenu.AddMenu("Seç", Sec);
            gm.GridMenu.AddMenu("Bırak", Birak);
            gridView1.BestFitColumns();
            gridView1.FormatAsNumber(0, "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "Numune", "Iptal", "Kalite2", "Kalite3", "Kalan1Kalite", "EksikAdet");


        }

        void Sec(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            if (gleAktarılacakDepo.EditValue.ToInt() == 0)
            {
                XtraMessageBox.Show("Lütfen transfer edilecek depoyu veya nakliyeciyi seçin.");
                return;
            }

            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Seç", true);

            if (gleDepo.EditValue.ToInt() != 6)
                gridView1.MakeReadOnly("A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            else
                gridView1.MakeReadOnly("Kalan1Kalite", "EksikAdet", "Numune", "Iptal", "Kalite2", "Kalite3");
        }

        void Birak(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Seç", false);
        }
        int DagilimId = 0;
        private void DagilimChanged()
        {
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();
                if (DagilimId != gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1))
                {
                    DagilimId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1);

                    MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                    if (r != null)
                        for (int i = 1; i < 29; i++)
                        {
                            string fieldName = String.Format("A{0:00}", i);
                            string cap = r[String.Format("A{0:00}", i)].ToString();
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        }
                }

            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
        }
    }
}
